
//
//  NSDate + Extension.swift
//  SinaMicroBlogSwift2
//
//  Created by dqw on 15/8/11.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

extension NSDate {
    
   class  func SinaDate(string string: String?) -> NSDate? {
        
        let df = NSDateFormatter()
        df.locale = NSLocale(localeIdentifier: "en")
        df.dateFormat = "EEE MMM dd HH:mm:ss zzz yyyy"
        return df.dateFromString(string!)
    }

    var dateDescription: String {
        let cal = NSCalendar.currentCalendar()
   
         if cal.isDateInToday(self) {
            let delta = Int (NSDate().timeIntervalSinceDate(self))
            if delta < 60 {
                return "刚刚"
            }
            if delta < 3600  {
                return "\(delta / 60) 分钟前"
            }
            return "\(delta / 3600) 小时前"
        }
            var format: String = " HH:mm"
        if cal.isDateInYesterday(self) {
            format = "昨天" + format
        }else {
            format = "MM-DD" + format
            let years = cal.components(NSCalendarUnit.Year, fromDate: NSDate(), toDate: self, options: NSCalendarOptions(rawValue: 0))
            if years.year > 0 {
                format = "yyyy-" + format
            }
        }
        let df = NSDateFormatter()
        df.locale = NSLocale(localeIdentifier: "en")
        df.dateFormat = format
      return  df.stringFromDate(self)
    }
    
    
    
}
